package study.chapter03.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//通过Callable实现多线程
public class TestThread4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> taskA = new FutureTask<>(new MyThread4("A"));
        FutureTask<String> taskB = new FutureTask<>(new MyThread4("B"));
        FutureTask<String> taskC = new FutureTask<>(new MyThread4("C"));

        new Thread(taskA).start();
        new Thread(taskB).start();
        new Thread(taskC).start();

        System.out.println("线程A执行结果：" + taskA.get());
        System.out.println("线程B执行结果：" + taskB.get());
        System.out.println("线程C执行结果：" + taskC.get());


    }
}

class MyThread4 implements Callable<String> {
    private String threadName;
    MyThread4(String threadName){
        this.threadName = threadName;
    }

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 5; i++) {
            System.out.println("线程" + threadName + ": " + i);
        }
        return "线程执行完毕";
    }
}
